<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by image.php
 * Date: 30.11.13
 * User: Lesh
 */

class Model_Image extends ORM
{
	protected $_created_column = array('column' => 'created', 'format' => TRUE);

	public function rules()
	{
		return array(
			'name' => array(
				array('Upload::valid'),
				array('Upload::not_empty'),
				array('Upload::type', array(':value', array('jpg', 'jpeg', 'png', 'gif'))),
				array('Upload::size', array(':value', '20M')),
				array(array($this, 'file_save'), array(':value'))
			),
		);
	}

	public function view_images($limit, $offset)
	{
		return $this->order_by('id', 'DESC')
			->limit($limit)
			->offset($offset)
			->find_all();
	}

	public function view_image($name)
	{
		return $this->where('uid', '=', $name)
			->find();
	}

	private function uploads_dir()
	{
		return PUBLIC_HTML.'upload'.DIRECTORY_SEPARATOR;
	}

	private function uploads_url()
	{
		return '/upload/';
	}

	public function file_save($file)
	{
		$type = strtolower(pathinfo(Arr::get($file, 'name'), PATHINFO_EXTENSION));
		$uid = md5(Arr::get($file, 'name').time());
		$uploaded = Upload::save($file, $uid.'.'.$type, $this->uploads_dir());
		if ($uploaded)
		{
			$this->save_image($uid.'.'.$type);
			$data = $this->data_image($this->uploads_dir().$uid.'.'.$type);
			$this->set('name', Arr::get($file, 'name'));
			$this->set('uid', $uid);
			$this->set('type', $type);
			$this->set('size', Arr::get($file, 'size'));
			$this->set('height', Arr::get($data, 'height'));
			$this->set('width', Arr::get($data, 'width'));
			$this->set('lon', Arr::get($data, 'lon'));
			$this->set('lat', Arr::get($data, 'lat'));
			$this->set('address', Arr::get($data, 'address'));
			$this->set('model', Arr::get($data, 'model'));
			$this->set('date_time_original', Arr::get($data, 'date'));
		}
		return $uploaded;
	}

	public function save_image($filename, $quality = 90)
	{
		Image::factory($this->uploads_dir().$filename)
			->resize(450, 450, Image::HEIGHT)
			->save($this->uploads_dir().'thumb/'.$filename, $quality);
		Image::factory($this->uploads_dir().$filename)
			->resize(200, 200, Image::INVERSE)
			->crop(200, 150)
			->save($this->uploads_dir().'crop/'.$filename, $quality);
	}
	public function resize_image($filename, $quality = 90)
	{
		Image::factory($this->uploads_dir().'image/'.$filename)
			->resize(140, Arr::get($_POST, 'res_select'), Image::HEIGHT)
			->save($this->uploads_dir().'image/'.$filename, $quality);
		Image::factory($this->uploads_dir().'thumb/'.$filename)
			->resize(150, 200, Image::HEIGHT)
			->save($this->uploads_dir().'thumb/'.$filename, $quality);
	}

	public function name_crop()
	{
		return $this->uploads_url().'crop/'.$this->uid.'.'.$this->type;
	}

	public function name_thumb()
	{
		return $this->uploads_url().'thumb/'.$this->uid.'.'.$this->type;
	}

	public function name_image()
	{
		return $this->uploads_url().$this->uid.'.'.$this->type;
	}

	public function description_all()
	{
		return $this->name.'<br>'.$this->description;
	}

	public function format_gps_data($gps_data, $lat_lon_ref)
	{
		$gps_info = array();
		foreach($gps_data as $gps)
		{
			list($j , $k) = explode('/', $gps);
			array_push($gps_info, $j/$k);
		}
		$coordination = $gps_info[0] + ($gps_info[1]/60.00) + ($gps_info[2]/3600.00);
		return (($lat_lon_ref == "S" || $lat_lon_ref == "W" ) ? '-'.$coordination : $coordination);
	}

	public function data_image($filename)
	{
		$details = exif_read_data($filename);

		$sections = explode(',', Arr::get($details, 'SectionsFound'));
		$gps_data = array();
		if(in_array('GPS', array_flip($sections)))
		{
			$lat = $this->format_gps_data(Arr::get($details, 'GPSLatitude'), Arr::get($details, 'GPSLatitudeRef'));
			$lon = $this->format_gps_data(Arr::get($details, 'GPSLongitude'), Arr::get($details, 'GPSLongitudeRef'));
			$info = json_decode(file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?language=ru&latlng=".$lat.",".$lon."&sensor=true"));
			$address = $info->results[0]->formatted_address;
			Arr::set_path($gps_data, 'lat', $lat);
			Arr::set_path($gps_data, 'lon', $lon);
			Arr::set_path($gps_data, 'address', $address);
		}
		if (isset($details['Make']))
		{
			Arr::set_path($gps_data, 'model', Arr::get($details, 'Make').' '.Arr::get($details, 'Model'));
		}
		if (isset($details['DateTimeOriginal']))
		{
			Arr::set_path($gps_data, 'date', strtotime(Arr::get($details, 'DateTimeOriginal')));
		}
		Arr::set_path($gps_data, 'height', Arr::path($details, 'COMPUTED.Height'));
		Arr::set_path($gps_data, 'width', Arr::path($details, 'COMPUTED.Width'));
		return $gps_data;
	}
}
